<template>
  <div class="app-container-tab c-width c-dis-flex-row c-hidden">
    <el-row :gutter="20" class="c-flex c-dis-flex-row c-hidden">
      <el-col :span="6" :xs="24" class="c-flex c-dis-flex-col c-hidden">
        <div class="com-tabs-buttons">
            <el-button
              type="primary"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
            >删除</el-button>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </div>
        <div class="head-container">
          <el-input
            v-model="labelName"
            placeholder="请输入标签名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 10px"
          />
        </div>
        <div class="com_tree_list c-flex c-scroll-y">
          <el-tree
            :data="labelOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="18" :xs="24"  class="c-dis-flex-col c-hidden com_tree_right_bg">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="c-flex c-scroll-y">
          <el-row>
            <el-col :span="12">
              <el-form-item label="标签编码" prop="labelCode">
                <el-input v-model="form.labelCode" placeholder="请输入标签编码" maxlength="30" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="标签名称" prop="labelName">
                <el-input v-model="form.labelName" placeholder="请输入标签名称" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上级标签" prop="parentId">
                <treeselect  no-options-text="无" v-model="form.parentId" :options="labelOptions" :show-count="true" placeholder="请选择上级标签" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序号" prop="orderNum">
                <el-input-number style="width: 100%;" v-model="form.orderNum"  placeholder="请输入排序号"  @change="toInteger" />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
        <el-col :span="24" class="com_form_btn_center">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
      </el-col>
    </el-col>
    </el-row>
  </div>
</template>

<script>
import { knownLedgeLabelTreeSelect, getKnowledgeLabel, delKnowledgeLabel, addKnowledgeLabel, updateKnowledgeLabel } from "@/api/questionbank/knowledgeLabel";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Cateogry",
  components: { Treeselect },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 题库分类选项
      labelOptions: undefined,
      // 分类名称
      labelName: undefined,
      // 表单参数
      form: {
        labelId:undefined,
        labelCode:undefined,
        labelName:undefined,
        parentId:undefined,
        orderNum:undefined
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        labelName: [
          { required: true, message: "标签名称不能为空", trigger: "blur" },
          { max: 20, message: '标签名称长度必须小于30个字符', trigger: 'blur' }
        ],
        orderNum:[{ required: true, message: "排序号不能为空", trigger: "blur" }]
      }
    };
  },
  watch: {
    // 根据分类名称筛选分类树
    labelName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getKnownledgeLabelTree();
  },
  methods: {
    toInteger() {
      let reg = /^[0-9]+$/
      if(!reg.test(this.form.orderNum)){
        this.$message.warning("只能输入整数排序")
        // 用以在dom渲染挂载后重新触发dom渲染挂载
        this.$nextTick(() => {
          this.form.orderNum = parseInt(this.form.orderNum)
        })
      }
    },
    /** 查询知识库标签树结构 */
    getKnownledgeLabelTree() {
      knownLedgeLabelTreeSelect().then(response => {
        this.labelOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.labelId=data.id;
      getKnowledgeLabel(data.id).then(response => {
        this.form = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        labelId: undefined,
        parentId: undefined,
        labelCode: undefined,
        labelName: undefined,
        orderNum: undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.labelId != undefined) {
            updateKnowledgeLabel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.getKnownledgeLabelTree();
            });
          } else {
            addKnowledgeLabel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.getKnownledgeLabelTree();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      if(this.form.labelId == undefined){
        this.$modal.msgError("请先选择要删除的节点!");
        return;
      }
      const labelId =parseInt(this.form.labelId);
      this.$modal.confirm('是否确认删除"' + this.form.labelName+ '"的数据项？').then(function() {
        return delKnowledgeLabel([labelId]);
      }).then(() => {
        this.getKnownledgeLabelTree()
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        this.$modal.msg('已取消删除');
      });
    }
  }
};
</script>
